package io.geekidea.boot.noguaranteeorder.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.geekidea.boot.area.dto.AreaDto;
import io.geekidea.boot.area.entity.Area;
import io.geekidea.boot.area.query.AreaQuery;
import io.geekidea.boot.area.service.AreaService;
import io.geekidea.boot.common.GroupValidator;
import io.geekidea.boot.common.PageDto;
import io.geekidea.boot.framework.response.ApiResult;
import io.geekidea.boot.noguaranteeorder.dto.NoGuaranteeOrderDto;
import io.geekidea.boot.noguaranteeorder.entity.NoGuaranteeOrder;
import io.geekidea.boot.noguaranteeorder.query.NoGuaranteeOrderQuery;
import io.geekidea.boot.noguaranteeorder.service.NoGuaranteeOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/NoGuaranteeOrder")
@Tag(name = "保外订单子类型订单管理")
public class NoGuaranteeOrderController {
    @Resource
    private NoGuaranteeOrderService noGuaranteeOrderService;

    @PostMapping("/add")
    @Operation(summary = "添加保外订单子类型")
    public ApiResult addNoGuaranteeOrder(@Validated(value = {GroupValidator.Insert.class}) @RequestBody NoGuaranteeOrderDto noGuaranteeOrderDto) throws Exception {
        NoGuaranteeOrder noGuaranteeOrderEntity = new NoGuaranteeOrder();
        BeanUtil.copyProperties(noGuaranteeOrderDto, noGuaranteeOrderEntity);
        noGuaranteeOrderService.save(noGuaranteeOrderEntity);
        return ApiResult.success("添加保外订单子类型成功！");

    }

    @PostMapping("/delete/{id}")
    @Operation(summary = "删除特定订单")
    public ApiResult removeNoGuaranteeOrder(@PathVariable(value = "id") Integer id) {
        noGuaranteeOrderService.removeById(id);
        return ApiResult.success("删除订单数据成功！");
    }

    @PostMapping("/update")
    @Operation(summary = "修改保外订单")
    public ApiResult updateNoGuaranteeOrder(@Validated(value = {GroupValidator.Update.class}) @RequestBody NoGuaranteeOrderDto noGuaranteeOrderDto) throws Exception {
        NoGuaranteeOrder noGuaranteeOrderEntity = new NoGuaranteeOrder();
        BeanUtil.copyProperties(noGuaranteeOrderDto, noGuaranteeOrderEntity);
        noGuaranteeOrderService.updateById(noGuaranteeOrderEntity);
        return ApiResult.success("修改订单数据成功！");
    }

    @PostMapping("/get/{id}")
    @Operation(summary = "查询特定订单")
    public ApiResult getNoGuaranteeOrderById(@PathVariable(value = "id") Integer id) {
        final NoGuaranteeOrder noGuaranteeOrder = noGuaranteeOrderService.getById(id);
        return ApiResult.success(noGuaranteeOrder);
    }

    /**
     * 按照条件组合分页查询订单数据列表
     *
     * @return
     */
    @PostMapping("/list")
    @Operation(summary = "根据状态分页查询")
    /**
     * 根据状态条件进行分页查询。
     *
     * @param areaQuery 区域查询条件
     * @return 包含区域分页信息的ApiResult
     */
    public ApiResult searchPage(@RequestBody NoGuaranteeOrderQuery noGuaranteeOrderQuery) {
        // 创建分页对象
        Page<NoGuaranteeOrder> page = new Page<>(noGuaranteeOrderQuery.getPage(), noGuaranteeOrderQuery.getSize());

        // 创建Lambda查询包装器
        LambdaQueryWrapper<NoGuaranteeOrder> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        // 根据区域编码进行精确查询
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(noGuaranteeOrderQuery.getState()), NoGuaranteeOrder::getState, noGuaranteeOrderQuery.getState());


        // 调用区域服务进行分页查询
        Page<NoGuaranteeOrder> pageDto = noGuaranteeOrderService.page(page, lambdaQueryWrapper);

        // 封装查询结果为自定义的分页DTO对象
        PageDto<NoGuaranteeOrder> pageInfo = new PageDto<>(pageDto.getRecords(), pageDto.getTotal(), pageDto.getSize(), pageDto.getCurrent());

        // 返回成功的ApiResult，携带分页信息
        return ApiResult.success(pageInfo);
    }
}
